﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XXN.Model;
using XXN.Model.Model;

namespace XXN.Service
{
  public class ProductService
    {
        Shopping_MallEntities db = new Shopping_MallEntities();
        public List<ProductModel> GetProduct(int page, int limit, string productName, string productType, out int count)
        {
            var pro = from item in db.Products.Where(p => true)
                      select new ProductModel
                      {
                          Pro_id = item.Pro_id,
                          Pro_name = item.Pro_name,
                          Pro_cat_id = item.Pro_cat_id,
                          Pro_cat_name = item.Procate.Pro_cat_name,
                          Number = item.Number,
                          People = item.People,
                          Status=item .Status ,
                          Shelftime = item.Shelftime,
                          Img = item.Img,
                          Rec_id=item .Rec_id,
                          Rec_Name = item.Recommend.Positionname,
                          Weight =item .Weight 
                      };
            count = pro.Count();
            if (!string.IsNullOrWhiteSpace(productName))
            {
                pro = pro.Where(p => p.Pro_name.Contains(productName));
            }
            if (!string.IsNullOrWhiteSpace(productType))
            {
                pro = pro.Where(p => p.Pro_cat_name == productType);
            }
            pro = pro.Where(p => p.Status.Equals("上架") || p.Status.Equals("下架"));
            pro = pro.OrderBy(p => p.Pro_id);
            pro = pro.Skip((page - 1) * limit).Take(limit);
            return pro.ToList();
        }
        public int AddInfo(Product pro)
        {
            db.Products.Add(pro);
            return db.SaveChanges();
        }
        public ProductModel EditInfo(int pro_id)
        {
            var pro = from item in db.Products.Where(p => p.Pro_id==pro_id)
                      select new ProductModel
                      {
                          Pro_id = item.Pro_id,
                          Pro_name = item.Pro_name,
                          Pro_cat_id = item.Pro_cat_id,
                          Pro_cat_name = item.Procate.Pro_cat_name,
                          Number = item.Number,
                          People = item.People,
                          Status = item.Status,
                          Shelftime = item.Shelftime,
                          Img = item.Img,
                          Rec_id = item.Rec_id,
                          Rec_Name=item.Recommend.Positionname,
                          Weight = item.Weight
                      };
            return pro.SingleOrDefault();
        }
        public int EditInfo(Product pro)
        {
            db.Entry(pro).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
        public List<Product> GetInfos()
        {
            return db.Products.ToList();
        }
        public int DelInfo(int pro_id)
        {
            db.Products.SingleOrDefault(x => x.Pro_id == pro_id).Status = "已删除";
            return db.SaveChanges();
        }
        public int OnStatus(int pro_id)
        {
            db.Products.SingleOrDefault(x => x.Pro_id  == pro_id).Status = "上架";
            return db.SaveChanges();
        }
        public int OffStatus(int pro_id)
        {
            db.Products.SingleOrDefault(x => x.Pro_id == pro_id).Status = "下架";
            return db.SaveChanges();
        }
        public List<Recommend> RecommendInfo()
        {
            return db.Recommends.ToList();
        }
        public int RecommendPro(Product pro)
        {
            db.Entry(pro).State = System.Data.Entity.EntityState.Modified;
            return db.SaveChanges();
        }
    }
}
